﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;

public partial class Exam_shiti : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            Session["sTittle"] = "老师出题";
            txtid.Text = Request.QueryString["zid"];
            Conn znothconn = new Conn();
            string znothsql = "select name from kecheng where id=" + txtid.Text;
            txtname.Text = znothconn.sqlFirst(znothsql);    //取得课程名称
            znothconn.Dispose();
            txtscore.Text = "2";
            txtshijian.Text = "20";
            if (Ddltaoti.Items.Count > 0)
            {
                Ddltaoti.SelectedIndex = 0;
            }
        }
        if (ddlType.SelectedIndex == 0)
        {
            Panel1.Visible = true;
            Panel2.Visible = false;
        }
        else
        {
            if (ddlType.SelectedIndex == 1)
            {
                Panel1.Visible = false;
                Panel2.Visible = true;
            }
            else
            {
                Panel1.Visible = false;
                Panel2.Visible = false;
            }
        }
        

    }

    

    protected void btnAdd_Click(object sender, EventArgs e)
    {
        if (txtExamTitle.Text.Trim().Length ==0)
        {
            Response.Write("<script language=javascript>alert('请输入考试题目,否则无法添加试题,按任意键继续.....');if(opener!=null){opener.location.reload();window.close();}else{document.location.href='shiti.aspx?zid="+ txtid.Text.Trim() +"&zname=" + txtname.Text.Trim()+"'}</script>");
            return;
        }
        if (Ddltaoti.SelectedIndex < 0)
        {
            Response.Write("<script language=javascript>alert('未选择套题或未建立套题,请先建立一个套题！！');if(opener!=null){opener.location.reload();window.close();}else{document.location.href='shiti.aspx?zid=" + txtid.Text.Trim() + "&zname=" + txtname.Text.Trim() + "'}</script>");
            return;
        }
        string sstr1 = txtid.Text.ToString().Trim();
        string sstr2 = Ddltaoti.SelectedValue;
        string zsql="";
        if (ddlType.SelectedIndex == 0) //单选
        {

            if (txtExamA.Text.Trim().Length * txtExamB.Text.Trim().Length * txtExamC.Text.Trim().Length * txtExamD.Text.Trim().Length==0)
            {
                Response.Write("<script language=javascript>alert('请输入考试选项,否则无法添加试题,按任意键继续.....');if(opener!=null){opener.location.reload();window.close();}else{document.location.href='shiti.aspx?zid=" + txtid.Text.Trim() + "&zname=" + txtname.Text.Trim() + "'}</script>");
                return;
            }
            zsql = "insert into tb_Questions"
                + "(que_lessonid,que_taotiid,que_subject,que_type,optionA,optionB,optionC,optionD,que_answer,note,xtsj1,score)"
                + "values('" + sstr1 + "','" + sstr2 + "','" + this.txtExamTitle.Text
                + "','" + ddlType.Text + "','" + this.txtExamA.Text + "','" + this.txtExamB.Text
                + "','" + this.txtExamC.Text + "','" + this.txtExamD.Text + "','"
                + ddlAnswer.Text + "','" + txtInstruction.Text + "','"+ DateTime.Now.ToString() +"',"+txtscore.Text+")";
            
        }
        if (ddlType.SelectedIndex == 1) //多选
        {
            if (txtExamA.Text.Trim().Length * txtExamB.Text.Trim().Length * txtExamC.Text.Trim().Length * txtExamD.Text.Trim().Length == 0)
            {
                Response.Write("<script language=javascript>alert('请输入考试选项,否则无法添加试题,按任意键继续.....');if(opener!=null){opener.location.reload();window.close();}else{document.location.href='shiti.aspx?zid=" + txtid.Text.Trim() + "&zname=" + txtname.Text.Trim() + "'}</script>");
                return;
            }
            string zanswer="";
            if (cblAnswer.Items[0].Selected == true)
            {
                zanswer = "A,";
            }

            if (cblAnswer.Items[1].Selected == true)
            {
                zanswer = zanswer+"B,";
            }
            if (cblAnswer.Items[2].Selected == true)
            {
                zanswer = zanswer + "C,";
            }
            if (cblAnswer.Items[3].Selected == true)
            {
                zanswer = zanswer + "D,";
            }

            zsql = "insert into tb_Questions"
                + "(que_lessonid,que_taotiid,que_subject,que_type,optionA,optionB,optionC,optionD,que_answer,note,xtsj1,score)"
                + "values('" + sstr1 + "','" + sstr2 + "','" + this.txtExamTitle.Text
                + "','" + ddlType.Text + "','" + this.txtExamA.Text + "','" + this.txtExamB.Text
                + "','" + this.txtExamC.Text + "','" + this.txtExamD.Text + "','"
                + zanswer + "','" + txtInstruction.Text + "','" + DateTime.Now.ToString() + "',"+txtscore.Text+")";

        }
        if (ddlType.SelectedIndex == 2) //问答
        {

            zsql = "insert into tb_Questions"
                + "(que_lessonid,que_taotiid,que_subject,que_type,note,xtsj1,score)"
                + "values('" + sstr1 + "','" + sstr2 + "','" + this.txtExamTitle.Text
                + "','" + ddlType.Text + "','"  + txtInstruction.Text + "','" + DateTime.Now.ToString() + "',"+txtscore.Text+")";

        }
        Conn zconn = new Conn();
        int noth=zconn.ExeSql(zsql);
        if (noth == 1)
        {
            Response.Write("<script language=javascript>alert('操作正确,添加题目成功！！');if(opener!=null){opener.location.reload();window.close();}else{document.location.href='shiti.aspx?zid=" + txtid.Text.Trim() + "&zname=" + txtname.Text.Trim() + "'}</script>");
        }
        else
        {
            Response.Write("<script language=javascript>alert('添加试题出错,请联络系统管理员！！');if(opener!=null){opener.location.reload();window.close();}else{document.location.href='shiti.aspx?zid=" + txtid.Text.Trim() + "&zname=" + txtname.Text.Trim() + "'}</script>");
        }
        zconn.Dispose();
        
    }
    
    protected void btnSelect_Click(object sender, EventArgs e)
    {
        
        string zxdurl;
        zxdurl = "shiti_result.aspx?zxdpkey=" + txtSelect.Text.Trim() + "&zxdpkechengid=" + txtid.Text.Trim() + "&zxdpname=" + txtname.Text.Trim() + "&zxdptype=" + ddlType1.SelectedValue;
        Response.Redirect(zxdurl);

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        //增加套题
        //首先判断是否输入了考试时间和套题名称
        if (txtshijian.Text.Trim().Length == 0 | txtQueName.Text.Trim().Length == 0)
        {
            Response.Write("<script language=javascript>alert('请输入考试时间和套题名称再添加...');if(opener!=null){opener.location.reload();window.close();}else{document.location.href='shiti.aspx?zid=" + txtid.Text.Trim() + "&zname=" + txtname.Text.Trim() + "'}</script>");
            return;
        }
        //判断完毕
        Conn zconn = new Conn();
        string zsql0 = "select count(id) from tb_TaoTi where LessonID='" + txtid.Text.Trim() + "' AND name='" + txtQueName.Text.Trim() + "'";
        string zresult=zconn.sqlFirst(zsql0);
        if (zresult == "0")
        {
            string zsql = "insert into tb_TaoTi (name,LessonID,writer,jointime,shijian)values('"
                + this.txtQueName.Text + "'," + this.txtid.Text + ",'" + Session["sUserName"].ToString() + "','" + DateTime.Now.ToString() +"',"+txtshijian.Text+ ")";
            int noth=zconn.ExeSql(zsql);
            if (noth == 1)
            {
                Response.Write("<script language=javascript>alert('操作正确,套题添加成功！！');if(opener!=null){opener.location.reload();window.close();}else{document.location.href='shiti.aspx?zid=" + txtid.Text.Trim() + "&zname=" + txtname.Text.Trim() + "'}</script>");
                Ddltaoti.DataBind();
                
            }
            else
            {
                Response.Write("<script language=javascript>alert('操作中出现故障,请联系系统管理员...');if(opener!=null){opener.location.reload();window.close();}else{document.location.href='shiti.aspx?zid=" + txtid.Text.Trim() + "&zname=" + txtname.Text.Trim() + "'}</script>");
            }
        }
        else
        {
            Response.Write("<script language=javascript>alert('套题名称重复,请重新命名！！');if(opener!=null){opener.location.reload();window.close();}else{document.location.href='shiti.aspx?zid=" + txtid.Text.Trim() + "&zname=" + txtname.Text.Trim() + "'}</script>");
        }
        
        zconn.Dispose();
    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        //Page.Response.Redirect("shiti.aspx");
        Page.Response.Redirect("shiti.aspx?zid=" + txtid.Text.Trim() + "&zname=" + txtname.Text.Trim());
    }

    protected void cmdclearscore_Click(object sender, EventArgs e)
    {
        txtSingleScore.Text = "";
        txtMultyScore.Text = "";
        txtQAScore.Text = "";
        txtTotalScore.Text = "";
    }
    protected void cmdTotal_Click(object sender, EventArgs e)
    {
        txtSingleScore.Text = "";
        txtMultyScore.Text = "";
        txtQAScore.Text = "";
        txtTotalScore.Text = "";
        
//        string zsql = "select sum(score) from tb_questions where (que_lessonid=" + txtid.Text.Trim() + "and que_taotiid=" + Ddltaoti.SelectedValue.ToString().Trim()+ ") group by que_type";
//        //统计分数的时候出错，问题出在我一个假设，用户三套题都出，并且按照单选、多选、问答的次序。一旦只有一种类型且不是单选，则分数位置次序必然出错，甚至出现分数为null的情况导致程序出错。
//简单而直接地做这件事情，分别计算单选、多选、问答题目的分数！
        //SqlDataReader reader1=zconn.Reader(zsql);
        Conn zconn = new Conn();
        string zsql = "select sum(score) from tb_questions where que_lessonid=" + txtid.Text.Trim() + "and que_taotiid=" + Ddltaoti.SelectedValue.ToString().Trim() + "and que_type='单选题'";
        //先计算单选题的分数。然后再分别计算多选、问答
        int ztotal=0;   //累计总分
        string znoth;      //记录计算结果
        znoth = zconn.sqlFirst(zsql);//简答题总分
        
        if (znoth != "0")
        {
            txtSingleScore.Text = znoth.ToString();
            ztotal += Int16.Parse(znoth);
        }
        zsql = "select sum(score) from tb_questions where que_lessonid=" + txtid.Text.Trim() + "and que_taotiid=" + Ddltaoti.SelectedValue.ToString().Trim() + "and que_type='多选题'";
        znoth = zconn.sqlFirst(zsql);//简答题总分

        if (znoth != "0")
        {
            txtMultyScore.Text = znoth.ToString();
            ztotal += Int16.Parse(znoth);
        }
        zsql = "select sum(score) from tb_questions where que_lessonid=" + txtid.Text.Trim() + "and que_taotiid=" + Ddltaoti.SelectedValue.ToString().Trim() + "and que_type='问答题'";
        znoth = zconn.sqlFirst(zsql);//简答题总分

        if (znoth != "0")
        {
            txtQAScore.Text = znoth.ToString();
            ztotal += Int16.Parse(znoth);
        }
        txtTotalScore.Text = ztotal.ToString();
        zconn.Dispose();
    }
}
